package by.grsu.vashchilo.page.administrator;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.vashchilo.app.AirlineWebApplication;
import by.grsu.vashchilo.dao.impl.TripDao;
import by.grsu.vashchilo.entity.Trip;
import by.grsu.vashchilo.page.administrator.TripEditPage;
import by.grsu.vashchilo.page.administrator.ListTripPage;

public class ListTripPage extends WebPage {

	public ListTripPage() {
		super();

		final TripDao tripDao = new TripDao(
				AirlineWebApplication.DATA_XML_FOLDER);

		final List<Trip> allTrips = tripDao.getAll();

		add(new DataView<Trip>("table-rows", new ListDataProvider<Trip>(
				allTrips)) {
			@Override
			protected void populateItem(final Item<Trip> item) {
				final Trip trip = item.getModelObject();

				item.add(new Label("id", trip.getId()));
				item.add(new Label("where", trip.getWhere()));
				item.add(new Label("timeofdeparture", trip.getTimeOfDeparture()));
				item.add(new Label("price", trip.getPrice()));
				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new TripEditPage(trip));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						tripDao.delete(trip.getId());
						setResponsePage(new ListTripPage());
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new TripEditPage(new Trip()));
			}
		});
	}

}
